package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.*;
import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.ActivityService;
import cn.gjculture.shopping.api.service.DiscountService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    ActivityDao activityDao;

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    CommonDeleteMapper commonDeleteDao;

    @Autowired
    DiscountService discountService;

    @Override
    public int insert(Activity activity) {
        activity.setActivityId(UUIDUtil.getUUID());
        activity.setInsertTime(DateUtil.getTimestamp());
        activity.setUpdateTime(DateUtil.getTimestamp());
        this.insertGoodsTypes(activity);
        this.insertGoodss(activity);
        discountService.insertDiscounts(activity);

        return activityDao.insert(activity);
    }


    private void insertGoodss(Activity activity) {
        List<String> goodss = activity.getGoodsIds();
        if (goodss != null && goodss.size() > 0) {
            List<Activity.ActivityGoods> activityGoodsList = new ArrayList<>(goodss.size());
            for (String id : goodss) {
                Activity.ActivityGoods aagds = new Activity.ActivityGoods(activity.getActivityId(), id);
                activityGoodsList.add(aagds);
            }
            activityMapper.insertGoodss(activityGoodsList);
        }
    }

    private void insertGoodsTypes(Activity activity) {
        List<String> goodsTypes = activity.getGoodsTypeIds();
        if (goodsTypes != null && goodsTypes.size() > 0) {
            List<Activity.ActivityGoodsType> activityGoodsTypeList = new ArrayList<>(goodsTypes.size());
            for (String gds : goodsTypes) {
                Activity.ActivityGoodsType aags = new Activity.ActivityGoodsType(activity.getActivityId(), gds);
                activityGoodsTypeList.add(aags);
            }
            activityMapper.insertGoodsTypes(activityGoodsTypeList);
        }
    }

    @Override
    public List<Activity> query() {
        List<Activity> activities = activityDao.query();
        for (Activity a : activities) {
            this.collect(a);
        }

        return activities;
    }

    private void collect(Activity a) {
        List<String> goodsTypes = activityDao.queryGoodsTypeIds(a.getActivityId());
        if (goodsTypes != null && goodsTypes.size() > 0) {
            a.setGoodsTypeIds(goodsTypes);
        }
        List<String> goodss = activityDao.queryGoodsIds(a.getActivityId());
        if (goodss != null && goodss.size() > 0) {
            a.setGoodsIds(goodss);
        }
        List<Discount> discounts = discountService.getByActivityId(a.getActivityId());
        if (discounts != null && discounts.size() > 0) {
            a.setDiscounts(discounts);
        }
    }

    @Override
    public Activity queryById(String activityId) {
        Activity activity = activityDao.queryById(activityId);
        this.collect(activity);
        return activity;
    }

    @Override
    public int deleteMore(List<String> activitys) {
        activityMapper.deleteGoodsTypes(activitys);
        activityMapper.deleteGoodss(activitys);
        discountService.deleteByActivitys(activitys);
        return commonDeleteDao.deleteActivity(activitys);
    }

    @Override
    public int update(Activity activity) {
        activity.setUpdateTime(DateUtil.getTimestamp());
        activityMapper.update(activity);
        //删除
        activityDao.deleteGoodsType(activity.getActivityId());
        activityDao.deleteGoods(activity.getActivityId());
        discountService.deleteByActivity(activity.getActivityId());
        //增加
        this.insertGoodsTypes(activity);
        this.insertGoodss(activity);
        discountService.insertDiscounts(activity);
        return 1;
    }

    @Override
    public List<Activity> queryValidActivity() {
        List<Activity> activities = activityDao.queryValidActivity();
        for (Activity a : activities) {
            this.collect(a);
        }
        return activities;
    }

    @Override
    public void queryActivityByGoods(Goods goods) {
        List<Activity> activities = this.queryValidActivity();
        for (Activity activity : activities) {
            if (activity.getEvent() != Activity.Event.MARKETING.getValue()) {
                continue;
            }
            if (activity.getCommodityType().equals("0")) {
                goods.getActivities().add(activity);
            } else {
                String[] commTypes = activity.getCommodityType().split(",");
                if (commTypes.length > 0 && ArrayUtils.contains(commTypes, String.valueOf(Commodity.CommodityType.GOODS.getValue()))) {
                    List<String> goodsTypes = activity.getGoodsTypeIds();
                    if (goodsTypes != null && goodsTypes.size() == 1 && goodsTypes.get(0).equals("0")) {
                        goods.getActivities().add(activity);
                    } else if (goodsTypes != null && goodsTypes.size() > 0) {
                        for (String id : goodsTypes) {
                            if (goods.getGoodsTypeId().equals(id)) {
                                List<String> goodsIds = activity.getGoodsIds();
                                if (goodsIds != null && goodsIds.size() == 1 && goodsIds.get(0).equals("0")) {
                                    goods.getActivities().add(activity);
                                } else if (goodsTypes != null && goodsTypes.size() > 0) {
                                    for (String goodsId : goodsIds) {
                                        if (goods.getGoodsId().equals(goodsId)) {
                                            goods.getActivities().add(activity);
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {
                    continue;
                }
            }
        }
    }

}
